﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CtrLayer;
using ModelLayer;

namespace GUILayer
{
    public partial class GUIRegMovie : Form
    {
        MovieController movieCtr = new MovieController();




        public GUIRegMovie()
        {
            InitializeComponent();
            
        }

        private void buttonRegMovieBack_Click(object sender, EventArgs e)
        {
            Form newForm = new GUIStart();
            this.Hide();
            newForm.ShowDialog();
            this.Close(); 
        }

        private void buttonRegMovieCreate_Click(object sender, EventArgs e)
        {
            bool active = false;
            if (checkBoxActiveRegMovieActivity.Checked)
                active = true;


            try
            {
                if (movieCtr.AddMovie(active, textRegMovieTitle.Text, Int32.Parse(textRegMoviePlaytime.Text), textRegMovieActors.Text, Int32.Parse(textRegMovieMinAge.Text), textBoxDescription.Text, "", textRegMovieDirector.Text, textRegMovieGenre.Text,
                textRegMovieLang.Text, decimal.Parse(textBoxRegMoviePrice.Text), textRegMovieLicensee.Text, dateTimePickerRegMovieStartDate.Value, dateTimePickerRegMovieDeadline.Value) >= 0)
                {
                    labelRegMovieNotifier.Text = "Filmen er oprettet";
                }
                else
                    labelRegMovieNotifier.Text = "Filmen er ikke oprettet";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Indtast alle felter.");
            }

            
        }

        private void buttonRegMovieRead_Click(object sender, EventArgs e)
        {
            string input = textRegMovieTitle.Text.Trim();
            try
            {
                if (input.Length == 0)
                    labelRegMovieNotifier.Text = "Indtast titel";
                else
                    PutValuesOnScreen(movieCtr.GetMovie(input));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Der er ingen film med valgte titel.");
            }
        }

        private void PutValuesOnScreen(Movie m)
        {

            labelRegMovieId.Text =  "" + m.Id;
            checkBoxActiveRegMovieActivity.Checked = m.Active;
            textRegMovieTitle.Text = m.Title;
            textRegMoviePlaytime.Text = "" + m.PlayLength;
            textRegMovieActors.Text = m.Actor;
            textBoxDescription.Text = m.Description;
            textRegMovieDirector.Text = m.Director;
            textRegMovieGenre.Text = m.Genre;
            textRegMovieLang.Text = m.Language;
            textRegMovieMinAge.Text = "" + m.MinAge;
            textRegMovieLicensee.Text = m.Licencee;
            dateTimePickerRegMovieStartDate.Value = m.RentalDateStart;
            dateTimePickerRegMovieDeadline.Value = m.RentalDateEnd;
            textBoxRegMoviePrice.Text = "" + m.Price;

        }

        private void buttonRegMovieUpdate_Click(object sender, EventArgs e)
        {
            bool active = false;
            if (checkBoxActiveRegMovieActivity.Checked)
                active = true;

            string actor = textRegMovieActors.Text;
            string[] actors = actor.Split(new Char[] { ',' });

            try
            {
                if (movieCtr.UpdateMovie(Int32.Parse(labelRegMovieId.Text), active, textRegMovieTitle.Text, Int32.Parse(textRegMoviePlaytime.Text), textRegMovieActors.Text, Int32.Parse(textRegMovieMinAge.Text), textBoxDescription.Text, "", textRegMovieDirector.Text, textRegMovieGenre.Text,
                textRegMovieLang.Text, decimal.Parse(textBoxRegMoviePrice.Text), textRegMovieLicensee.Text, dateTimePickerRegMovieStartDate.Value, dateTimePickerRegMovieDeadline.Value) >= 0)
                {
                    labelRegMovieNotifier.Text = "Filmen er redigeret";
                }
                else
                    labelRegMovieNotifier.Text = "Filmen er ikke redigeret";
                }
            catch(Exception ex)
            {
                MessageBox.Show("Vælge en film.");
            }
        }

        private void buttonRegMovieDelete_Click(object sender, EventArgs e)
        {
            Movie movie = movieCtr.GetMovie(textRegMovieTitle.Text);
            if (movie == null)
            {
                labelRegMovieNotifier.Text = "Film ikke fundet";
                ClearFields();
            }
            else
            {
                movieCtr.DeleteMovie(movie.Id);
                ClearFields();
                labelRegMovieNotifier.Text = "Filmen er slettet";
            }
        }

        private void buttonRegMovieClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void ClearFields()
        {
            labelRegMovieId.Text = "";
            checkBoxActiveRegMovieActivity.Checked = false;
            textRegMovieTitle.Text = "";
            textRegMoviePlaytime.Text = "";
            textRegMovieActors.Text = "";
            textBoxDescription.Text = "";
            textRegMovieDirector.Text = "";
            textRegMovieGenre.Text = "";
            textRegMovieLang.Text = "";
            textRegMovieMinAge.Text = "";
            textRegMovieLicensee.Text = "";
            textBoxRegMoviePrice.Text = "";
        }

        
    }
}
